package com.blb.blb_erp.controller;

import com.blb.blb_erp.entity.JsonResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @PostMapping("/login")
    public JsonResult login(String username,String password,Boolean rememberMe){
        //创建Token
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //设置记住我
        token.setRememberMe(rememberMe);
        //获得subject
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            return new JsonResult(1,"登录成功");
        }catch (AuthenticationException ex){
            ex.printStackTrace();
        }
        return new JsonResult(0,"账号或密码错误");
    }

    @RequiresRoles("管理员")
    @GetMapping("/role-admin")
    public String testRole(){
        return "有管理员角色";
    }

    @RequiresPermissions("部门管理")
    @GetMapping("/menu-dept")
    public String testMenu(){
        return "有部门管理权限";
    }
}
